package cn.aezo.minions.web.services.impl;

import cn.aezo.minions.gen.mapper.ModuleFunctionDao;
import cn.aezo.minions.gen.mapper.ProjectModuleDao;
import cn.aezo.minions.gen.model.ProjectModule;
import cn.aezo.minions.web.services.ProjectConfigService;
import cn.aezo.utils.base.ValidU;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by smalle on 2017/10/28.
 */
@Service
public class ProjectConfigServiceImpl implements ProjectConfigService {
    private static final List<ProjectModule> defaultProjectModules = new ArrayList() {{
        add(new ProjectModule(null, ProjectModule.DefaultModuleName, "默认", ""));
        add(new ProjectModule(null, "base", "基础", "base"));
    }};

    @Autowired
    private ProjectModuleDao projectModuleDao;

    @Autowired
    private ModuleFunctionDao moduleFunctionDao;

    @Transactional
    @Override
    public Object initModule(Long projectConfigId) {
        if(ValidU.isEmpty(projectConfigId)) return null;

        moduleFunctionDao.deleteByProjectConfigId(projectConfigId);
        projectModuleDao.deleteByProjectConfigId(projectConfigId);

        for (ProjectModule projectModuleItem : defaultProjectModules) {
            projectModuleItem.setProjectConfigId(projectConfigId);
            projectModuleDao.insert(projectModuleItem);
        }

        return null;
    }


}
